//
//  CHSUI2ViewController.m
//  ZXCategories_Example
//
//  Created by zhuxm2023 on 2024/8/1.
//  Copyright © 2024 海参. All rights reserved.
//

#import "CHSUI2ViewController.h"
#import <Masonry/Masonry.h>
#import <ZXCategoriesUtil/ZXCategoriesUtil.h>
#import "CHSUI2CollectionViewCell.h"

@interface CHSUI2ViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UITableView *tableView;
@property (atomic, strong) NSMutableArray *dataMArray;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation CHSUI2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUI];
    [self addMas];
    [self setData];

}

- (void)setUI{
    self.view.backgroundColor = [UIColor zx_colorWithHexString:@"#F5F5F5"];
    self.navigationItem.title = @"测试scrollView、tableView是否能同时上下滚动、水平滚动";
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.collectionView];
}


- (void)addMas{
    
    CGFloat top = UIApplication.zx_safeAreaNavBarHeight;
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {      make.left.mas_equalTo(self.view.mas_left).with.offset(15);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.view.mas_top).with.offset(top);
        make.height.mas_equalTo(@200);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {      make.left.mas_equalTo(self.view.mas_left).with.offset(15);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.scrollView.mas_bottom).with.offset(20);
        make.height.mas_equalTo(@200);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {      make.left.mas_equalTo(self.view.mas_left).with.offset(15);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.tableView.mas_bottom).with.offset(20);
        make.height.mas_equalTo(@200);
    }];
}

#pragma mark - Data

- (void)setData
{
   NSArray *arr = @[
        @{@"1":@"1"},
        @{@"2":@"2"},
        @{@"3":@"3"},
        @{@"4":@"4"},
        @{@"5":@"5"},
        @{@"6":@"6"},

    ];
    self.dataMArray = [NSMutableArray arrayWithArray:arr];
}

#pragma mark -

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor redColor];
        _scrollView.contentSize = CGSizeMake(600, 1000);
    }
    return _scrollView;
}



- (UITableView *)tableView
{
    if (!_tableView)
    {
        UITableView *view = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        view.backgroundColor = [UIColor redColor];
        view.delegate = self;
        view.dataSource = self;
        view.showsVerticalScrollIndicator = NO;
        [view registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        view.estimatedRowHeight = 87;
        view.rowHeight = UITableViewAutomaticDimension;
        if (@available(iOS 15.0, *)) {
            view.sectionHeaderTopPadding = 0;
        } else {
            // Fallback on earlier versions
        }
        view.contentSize = CGSizeMake(600, 1000);
        _tableView = view;
    }
    return _tableView;
}


#pragma mark - TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataMArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSDictionary *dic = [self.dataMArray objectAtIndex:indexPath.row];
    NSString *value = [dic.allValues firstObject];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = value;
    cell.contentView.backgroundColor = [UIColor greenColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 12;
    }
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 12;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc] init];
         layout.itemSize = CGSizeMake(380, 50);
         layout.minimumLineSpacing = 10;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
         layout.minimumInteritemSpacing = 10;
         layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        UICollectionView *view = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
//        view.delegate = self;
          view.dataSource = self;
          [view registerClass:[CHSUI2CollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
        view.backgroundColor = [UIColor whiteColor];
        _collectionView = view;
    }
    return _collectionView;
}

# pragma  mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CHSUI2CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    cell.label.text = [NSString stringWithFormat: @"%@",@(indexPath.item)];
    return cell;
}
@end
